import http from '../../http/http'
import * as url from '../../http/api'
const state = {
    czData: [],
    vipData: [{
        id: undefined,
        delFlag: undefined,
        name: undefined,
        password: undefined,
        sex: undefined,
        tel: undefined,
        idCard: undefined,
        createTime: undefined,
        updateTime: undefined,
        loginTime: undefined,
        member: {
            id: undefined,
            delFlag: undefined,
            userId: undefined,
            number: undefined,
            state: undefined,
            score: undefined,
            discount: undefined,
            level: undefined,
            money: undefined,
            freezeTime: undefined,
            lossTime: undefined,
            mergeTime: undefined,
            updateTime: undefined
        }
    }],
    rankdishesData: [],
    rankmemberData: [],
    arrangeData:[
        {workDay:"2020-04-02",staff:{name:"种子"}},
        {workDay:"2020-04-02",staff:{name:"佳能"}},
        {workDay:"2020-04-01",staff:{name:"佳能"}},
        {workDay:"2020-04-03",staff:{name:"种子"}}
    ],
    // 员工信息
    Staff:[]
}
const mutations = {
    //获取餐桌所有
    getAllData(context) {
        return http({
            url: url.BSGETCZDATA,
            method: "post",
        }).then(res => {
            // console.log(res);
            // console.log('获取添加完成的数据成功');
            state.czData = res.data  //将获取的数据保存到VUEX
        })
    },
    getAllVipData(context) {
        return http({
            url: url.BSGETVIPDATA,
            method: "post",
        }).then(res => {
            // console.log(res);
            // console.log('获取添加完成的数据成功');
            // state.vipData = res.data  //将获取的数据保存到VUEX
        })
    },
    //排行榜 菜品数据
    getrankdishesData(context, obj) {
        console.log(obj.data);
        
        state.rankdishesData = obj.data
    },
    //排行榜 会员消耗数据
    getrankmemberData(context, obj) {
        state.rankmemberData = obj
    },
    //操作排班数据
    operationArrangeData(context,obj){
        console.log(obj);
        
        state.arrangeData = obj
        state.arrangeData.forEach(item=>{
        let date = new Date(item.workDay);
        let year = date.getFullYear();
        let month = date.getMonth()+1;
        let day = date.getDate();
        month = month < 10 ? "0"+month:month;
        day = day < 10 ? "0"+day:day;
        item.workDay = year+'-'+month+'-'+day;
        })
    },
    //存入员工
    getStaffData(context,obj){
        // console.log(obj);
        
        state.Staff = obj
    }
}
const actions = {
    //获取餐桌数据
    getczData({ commit }) {
        return http({
            url: url.BSGETCZDATA,
            method: "post",
        })
            .then(res => {
                console.log(res);
                state.czData = res.data   //将获取的数据存放在VUEX里面
            })
            .catch(err => {
                console.log(err);
            })
    },
    //添加餐桌
    addczData({ commit }, obj) {
        return http({
            url: url.BSADDCZDATA,
            method: "post",
            data: obj
        }).then(res => {
            // console.log(res);
            if (res.status === 200) { //后台添加成功
                commit('getAllData')
                return 200
            }
        }).catch(err => {
            console.log(err)
        })
    },
    //餐桌编辑
    editczData({ commit }, obj) {
        // console.log(obj);
        return http({
            url: url.BSSETCZDATA,
            method: 'post',
            data: {
                "delFlag": 0,
                "id": obj.id,
                "number": obj.number,
                "peopleNum": obj.peopleNum,
                "state": obj.state,
                "type": obj.type,
                "typeId": 0
            }
        }).then(res => {
            console.log(res);
            return 200
        }).catch(err => {
            console.log(err);
        })
    },
    //搜索餐桌
    czsearchData({ commit }, obj) {
        let searchData = {}
        if (obj.region == "type") {
            searchData.type = obj.text
        }else if(obj.region == "id"){
            searchData.id = obj.text
        }
        return http({
            url:url.BSSEARCZDATA,
            method:'post',
            data:searchData
        }).then(res=>{
            console.log(res);
            state.czData = res.data
            return res.status
        }).catch(err=>{
            console.log(err);
        })
    },
    //获取会员数据
    getvipData({ commit }) {
        http({
            url: url.BSGETVIPDATA,
            method: "post",
        })
            .then(res => {
                console.log(res);
                state.vipData = res.data   //将获取的数据存放在VUEX里面
            })
            .catch(err => {
                console.log(err);
            })
    },
    //添加会员
    addvipData({ commit }, obj) {
        // console.log(obj);
        return http({
            url: url.BSADDVIPDATA,
            method: "post",
            data:{
                "createTime": obj.createTime,
                "idCard": obj.idCard,
                "member": {
                  "discount": obj.discount,
                  "level": obj.level,
                  "money": obj.money,
                  "number": obj.number,
                  "score": obj.score,
                  "state": obj.state,
                  "userId": obj.userId
                },
                "name": obj.name,
                "password": obj.password,
                "sex": obj.sex,
                "tel": obj.tel,
              }

            // data:{
            //     user:{
            //         name:"张三",
            //         password:'1233231',
            //         sex:1,
            //         tel:"321421",
            //         idCard:"3214",
            //         createTime:'2020-04-06 14:40:41'
            //     },
            //     member:{
            //         userId:9,
            //         number:"3213321",
            //         state:0,
            //         score:1,
            //         discount:'0.9',
            //         level:1,
            //         money:'200',
            //     }
            // }
        }).then(res => {
            console.log(res);
            if (res.status === 200) { //后台添加成功
                // commit('getAllVipData')
                // return 200
            }
        }).catch(err => {
            console.log(err)
        })
    },
    //用户充值
    userRecharge({ commit }, obj) {
        return http({
            url: url.BSVIPRECHARGE,
            method: 'post',
            data:obj
        }).then(res => {
            // console.log(res);
            return res.status
        }).catch(err => {
            console.log(err);

        })
    },
    //会员编辑信息
    editvipData({ commit }, obj) { //未解决
        console.log(obj);
        return http({
            url: url.BSVIPEDIT,
            method: 'post',
            data:{
                id:obj.id,
                number : obj.number,
                state : obj.state,
                discount : obj.discount,
                level : obj.level,
                updateTime : obj.updateTime,
            }
        }).then(res => {
            return res.status
        }).catch(err => {
            console.log(err);

        })

    },
    //会员搜索
    vipsearchData({ commit }, obj) { 
        let searchData = {}
        if (obj.region == "name") {
            searchData.name = obj.text
        }else if(obj.region == "id"){
            searchData.id = obj.text
        }
        return http({
            url: url.BSVIPSHAR,
            method: 'post',
            data:searchData
        }).then(res => {
            state.vipData = res.data
            return 200
        }).catch(err => {
            console.log(err);
        })
    },
    //营业排行请求搜索
    getyinyeData({ commit }, obj) {
        if(obj.region=='dishes'){
            return http({
                url: url.BSGETYYDATA,
                method: 'post',
            }).then(res=>{
                commit("getrankdishesData", res)
                return 200
            })
        }else if(obj.region == 'member'){
            return http({
                url: url.BSGETYYDATA,
                method: 'post',
            }).then(res=>{
                commit("getrankmemberData", res)
                return 200
            })
        }
    },
    //获取排班信息
    getArrangeData({commit}){
        return http({
            url:url.BSGETPBDATA,
            method:'post',
        }).then(res=>{
            commit('operationArrangeData',res.data)
        }).catch(err=>{
            console.log(err);
        })
    },
    //会员冻结
    freezeVip({commit},obj){
        // console.log(obj);
        return http({
            url:url.BSVIPFREEZE,
            method:'post',
            data:obj
        }).then(res=>{
            console.log(res.status);
            return res.status
        }).catch(err=>{
            console.log(err);
            
        })
    },
    //删除餐桌
    delCz({commit},obj){
        return http({
            url:url.BSDELCZDATA,
            method:'post',
            data:obj
        }).then(res=>{
            console.log(res);
            return res.status
        }).catch(err=>{
            console.log(err);
            
        })
    },
    //get员工信息
    getStaff({commit}){
        return http({
            url:url.BSGETSTAFF,
            method:'post',
        }).then(res =>{
            // console.log(res);
            if(res.status == 200){
                commit('getStaffData',res.data)
                return res.status
            }
        }).catch(err =>{
            console.log(err)
        })
    },
    //add排班员工
    setscheduling({commit},arr){
            return http({
                url:url.BSADDPBDATA,
                method:'post',
                data:arr
            }).then(res=>{
                // console.log(res);
                return res.status
            })
        
    },
    //根据日期修改排班人员
    xiugaischeduling({commit},obj){
        // let obj ={}
        // obj.workDay = "2020-04-11"
        // obj.timeSlot = 0,
        // obj.id = 3
        // obj.delFlag = 1
        // console.log(obj);
        console.log(obj);
        
        return http({
            url:url.BSSETPBDATA,
            method:'post',
            data:obj
        }).then(res=>{
            console.log(res);
        })
    }

}
const getters = {

}
export default {
    state,
    mutations,
    actions,
    getters
}